﻿/// <reference path="application.js" />

function Camera() {
    // Initialize camera properties
    // World coordinates
    this.x = 0;
    this.y = 0;

    this.targetX = 0;
    this.targetY = 500;

    this.velocity = 15;

    this.trackedEntity = null;

    if (typeof Camera.initialized == "undefined") {

        /// Activate tracking on an entity (the entity assumes it has x y fields)
        Camera.prototype.Track = function (entity) {
            this.trackedEntity = entity;
        };

        /// Stops the camera tracking
        Camera.prototype.StopTracking = function () {
            this.trackedEntity = null;
        };

        /// Determines whether the camera is in tracking mode
        Camera.prototype.GetIsTracking= function () {
            return trackedEntity != null;
        };

        /// Centers the camera at the specified point and deactivates tracking
        Camera.prototype.CenterOn = function (x, y) {
            this.targetX = x - app.context.container.width / 2;
            this.targetY = y + app.context.container.height / 2;
        };

        /// Moves the camera
        Camera.prototype.Move = function (x, y) {
            this.StopTracking();
            this.targetX += x;
            this.targetY += y;
        };

        /// Moves the camera right
        Camera.prototype.GoRight = function () {
            this.StopTracking();
            this.targetX += this.velocity;
        };

        /// Moves the camera right
        Camera.prototype.GoLeft = function () {
            this.StopTracking();
            this.targetX -= this.velocity;
        };

        /// Moves the camera right
        Camera.prototype.GoUp = function () {
            this.StopTracking();
            this.targetY += this.velocity;
        };

        /// Moves the camera right
        Camera.prototype.GoDown = function () {
            this.StopTracking();
            this.targetY -= this.velocity;
        };

        /// Camera think
        Camera.prototype.Think = function () {
            if(this.trackedEntity != null){
                this.CenterOn(this.trackedEntity.x, this.trackedEntity.y);
            }

            this.x += (this.targetX - this.x) * 0.3;
            this.y += (this.targetY - this.y) * 0.3;
        };

        Camera.initialized = true;
    }
}